<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input
            v-model="queryParams.modelName"
            placeholder="请输入模型名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型专业" prop="modelSpecialty">
        <el-input
            v-model="queryParams.modelSpecialty"
            placeholder="请输入模型专业"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传时间" prop="uploadTime">
        <el-date-picker clearable
                        v-model="queryParams.uploadTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择上传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="版本编号" prop="versionNumber">
        <el-input
            v-model="queryParams.versionNumber"
            placeholder="请输入版本编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['drawing:document:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['drawing:document:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['drawing:document:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['drawing:document:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="documentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="区域名称" align="center" prop="regionName"/>
      <el-table-column label="模型名称" align="center" prop="modelName"/>
      <el-table-column label="模型类型" align="center" prop="modelType"/>
      <el-table-column label="模型专业" align="center" prop="modelSpecialty"/>
      <el-table-column label="版本号" align="center" prop="versionNumber"/>
      <el-table-column label="文档编码" align="center" prop="documentCode"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['drawing:document:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['drawing:document:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改图档中心模型文档对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="documentForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="归属区域" prop="modelRegionId">
          <el-tree-select
              v-model="form.modelRegionId"
              :data="treeData"
              :props="defaultProps"
              value-key="id"
              placeholder="请选择归属区域"
              check-strictly
          />
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称"/>
        </el-form-item>
        <el-form-item label="模型专业" prop="modelSpecialty">
          <el-select v-model="form.modelSpecialty" placeholder="请输入模型类型">
            <el-option
                v-for="item in modelSpecialtyOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="模型类型" prop="modelType">
          <el-select v-model="form.modelType" placeholder="请输入模型类型">
            <el-option
                v-for="item in modelTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>

import {
  listDocument,
  getDocument,
  delDocument,
  addDocument,
  updateDocument,
  regionTree
} from "@/api/drawing/document";

const {proxy} = getCurrentInstance();

/**
 * 遮罩层
 *
 * @type {Ref<UnwrapRef<boolean>>}
 */
const loading = ref(true);

/**
 * 选中数组
 *
 * @type {Ref<UnwrapRef<*[]>>}
 */
const ids = ref([]);

/**
 * 非单个禁用
 *
 * @type {Ref<UnwrapRef<boolean>>}
 */
const single = ref(true);

/**
 * 非多个禁用
 *
 * @type {Ref<UnwrapRef<boolean>>}
 */
const multiple = ref(true);

/**
 * 显示搜索条件
 *
 * @type {Ref<UnwrapRef<boolean>>}
 */
const showSearch = ref(true);

/**
 * 总条数
 *
 * @type {Ref<UnwrapRef<number>>}
 */
const total = ref(0);

/**
 * 弹出层标题
 *
 * @type {Ref<UnwrapRef<string>>}
 */
const title = ref("");

/**
 * 是否显示弹出层
 *
 * @type {Ref<UnwrapRef<boolean>>}
 */
const open = ref(false);

/**
 * 部门名称
 *
 * @type {Ref<UnwrapRef<string>>}
 */
const deptName = ref("");

/**
 * 图档中心模型文档表格数据
 *
 * @type {Ref<UnwrapRef<*[]>>}
 */
const documentList = ref([]);

/**
 * 区域树
 *
 * @type {Ref<UnwrapRef<*[]>>}
 */
const treeData = ref([]);

/**
 * 区域树结构
 *
 * @type {{children: string, label: string}}
 */
const defaultProps = {
  children: 'children',
  label: 'regionName',
}

/**
 * 模型专业版本
 *
 * @type {[{label: string, value: string},{label: string, value: string}]}
 */
const modelSpecialtyOptions = [
  {
    label: '模型专业一类',
    value: '1',
  },
  {
    label: '模型专业二类',
    value: '2',
  },
]

/**
 * 模型类型版本
 *
 * @type {[{label: string, value: string},{label: string, value: string}]}
 */
const modelTypeOptions = [
  {
    label: '模型一类',
    value: '1',
  },
  {
    label: '模型二类',
    value: '2',
  },
]

/**
 * 表单数据
 *
 * @type {*}
 */
const data = reactive({
  form: {},
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    modelName: null,
    modelType: null,
    modelSpecialty: null,
    uploadTime: null,
    versionNumber: null
  },
  // 表单校验
  rules: {
    modelName: [{required: true, message: "模型名称不能为空", trigger: "blur"}],
    modelType: [{required: true, message: "模型类型不能为空", trigger: "change"}],
    modelSpecialty: [{required: true, message: "模型专业不能为空", trigger: "blur"}],
  }
});

const {queryParams, form, rules} = toRefs(data);

/**
 * 查询图档中心模型文档列表
 */
function getList() {
  loading.value = true;
  listDocument(queryParams.value).then(response => {
    documentList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}

/**
 * 获取区域树
 */
function getRegionTree() {
  regionTree().then(response => {
    treeData.value = response.data;
  });
}

/**
 * 取消按钮
 */
function cancel() {
  open.value = false;
  reset();
}

/**
 * 表单重置
 */
function reset() {
  form.value = {
    id: null,
    modelRegionId: null,
    modelName: null,
    modelType: null,
    modelSpecialty: null,
    uploadTime: null,
    versionNumber: null
  };
  proxy.resetForm("documentForm");
}

/**
 * 搜索按钮操作
 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/**
 * 重置按钮操作
 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

/**
 * 多选框选中数据
 *
 * @param selection 选择数据
 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/**
 * 新增按钮操作
 */
function handleAdd() {
  reset();
  getRegionTree();
  open.value = true;
  title.value = "新增图档中心模型文档";
}

/**
 * 修改按钮操作
 *
 * @param row 点击数据
 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getDocument(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改图档中心模型文档";
  });
}

/**
 * 提交按钮
 */
function submitForm() {
  proxy.$refs["documentForm"].validate(valid => {
    if (valid) {
      //修改
      if (form.value.id != null) {
        updateDocument(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
        return;
      }
      //新增
      addDocument(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/**
 * 删除按钮操作
 *
 * @param row 点击数据
 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  const modelName = row.modelName;
  proxy.$modal.confirm('确认删除模型名称为"' + modelName + '"的数据吗？').then(function () {
    return delDocument(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {

  });
}

/**
 * 导出按钮操作
 */
function handleExport() {
  this.download('drawing/document/export', {
    ...queryParams.value,
  }, `document_${new Date().getTime()}.xlsx`)
}

getList();
</script>
